'''
Created on 01/03/2013

@author: Elfi


--------------------------------
results for http://www.google.com

content size: 245.244 kb

http requests: 8
status 200: 7
status 302: 1

profiler timing:
3.079 secs (page load)
3.047 secs (network: end last request)
0.282 secs (network: end first request)

file extensions: (count, size)
js: 1, 19.541 kb
png: 3, 22.199 kb
unknown: 4, 203.504 kb

http timing detail: (status, method, doc, size, time)
302, GET, /, 222, 282 ms
200, GET, /tia.png, 387, 31 ms
200, GET, /rs=AItRSTNms2ONvlvAzESWBMEhOIZO2qqjHA, 846, 31 ms
200, GET, /chrome-48.png, 1834, 93 ms
200, GET, /fa6a39a64639448c.js, 19541, 343 ms
200, GET, /logo4w.png, 19978, 515 ms
200, GET, /, 29558, 531 ms
200, GET, /rs=AItRSTNms2ONvlvAzESWBMEhOIZO2qqjHA, 172878, 1656 ms

'''

from singletonmixin import Singleton

class GlobalHttpMetrics(Singleton):
    '''
    Statics container. It stores all the information about the connections made
    through the proxy in order to later present them in stdout
    '''


    def __init__(self):
        '''
        Constructor
        '''
        super(GlobalHttpMetrics, self).__init__()
        self.__url = ""
        self.__httpRequests = 0
        self.__totalTransfer = 0
        self.__status = {}
        self.__timeElapsed = 0
        self.__innerURLs = {}
        
        
    @property
    def url(self):
        return self.__url
    
    @url.setter
    def url(self,value):
        self.__url = value

    @property
    def httpRequests(self):
        return self.__httpRequests
    
    @httpRequests.setter
    def httpRequests(self,value):
        self.__httpRequests = value
        
    @property
    def totalTransfer(self):
        return self.__totalTransfer
    
    @totalTransfer.setter
    def totalTransfer(self,value):
        self.__totalTransfer = value

    @property
    def timeElapsed(self):
        return self.__timeElapsed
    
    @timeElapsed.setter
    def timeElapsed(self,value):
        self.__timeElapsed = value

    def statusCount(self, code):
        try:
            GlobalHttpMetrics.getInstance().__status[code] += 1
        except KeyError:
            GlobalHttpMetrics.getInstance().__status[code] = 1
                
    def siteResponseTime(self, command, content_type, status, time, length):
        GlobalHttpMetrics.getInstance().__innerURLs[command] = (content_type, status, time, length)

    def docid(self):
        import time
	# Only the first 64 bytes of the URL will be considered
        return "__stats__%s__%s" % (self.url[:64],time.strftime("%Y%m%d_%H%M%S",time.gmtime()))

    '''
    Print all the information contained in this instance
    '''    
    def stdPrint(self):
        print( "Profile for:\n%s\n-----------------------------------\n" % self.__url)
        print( "# of requests: %d" % self.__httpRequests )
        if self.__totalTransfer < 1024:
            print( "Total bytes transfered: %d B" % self.__totalTransfer )
        else:
            print( "Total bytes transfered: %.3f KB" % (float(self.__totalTransfer) / 1024.0) )
        for k,v in self.__status.iteritems():
            print("Status %s: %d" % (k,v))
        print( "\nTotal time elapsed: %f sec" % self.__timeElapsed )

        print( "URL response time (content_type, status, time)\n-----------------")
        for k,v in self.__innerURLs.iteritems():
            print("%s, %s, %f secs, %d --> %s" % (v[0],v[1],v[2],v[3],k))
            
